<?php
require_once( "$IP/includes/OutputPage.php" );

if ( ! defined( 'MEDIAWIKI' ) )
	die( 1 );

/**
 * @todo document
 */
class SoundshareOutputPage extends OutputPage {
	var $mInBocata = true;
	var $mCloud = '';
	var $mArtistDay = '';
	var $mGenres = '';
	var $mILikeThis = '';
	var $mDefaultTitle = '';
	var $mDidYouMean = '';
	var $mType = '';
	var $mMainSearch = false;
	var $mHasHeader = false;
	var $mWikiHeader = '';

	function __construct() {
		global $wgHooks;
		$wgHooks['SkinTemplateOutputPageBeforeExec'][] = $this;
	}	
	
	public function onSkinTemplateOutputPageBeforeExec( &$skin, &$tpl )
	{
		global $wgTitle, $wgRequest;
		$mode = true; $bocata = $no_bocata = '';
		$body = $this->mBodytext;

		if (!$this->mHasHeader) $body = "<!--LAY-H-->$body";

		$bocata = array(0 => ""); 
		$no_bocata = array(0 => "");

		$blocks = preg_split("/<!--LAY-(.)([^\>]*)-->/", $body, -1, PREG_SPLIT_DELIM_CAPTURE);
		$pos = 0;
		if ($blocks[0]!="") $bocata[0] = $blocks[0];

		$bocata_end = $no_bocata_end = '';

		for ($i = 1; $i<=count($blocks); $i+=3) {
			$output = true;
			switch ($blocks[$i]) {
				case "B":
					$mode = !$mode;
					break;
				case "O":
					$pos = (int)$blocks[$i+1];
					break;
				case "H":
					$content_actions = $tpl->data['content_actions'];


                                        //dont paint wiki menu if the page is user profile
					$header = null;
                                        $value = $wgRequest->getText( 'title' );
                                        $values = explode(":", $value);

                                        if ($values[0] != 'User') {

                                            $header = "<ul id='wiki-menu'>";

                                            foreach($content_actions as $key => $tab) {
                                                    $header .= '<li id="ca-' . Sanitizer::escapeId($key).'"';
                                                    if( $tab['class'] )
                                                            $header .= ' class="'.htmlspecialchars($tab['class']).'"';

                                                    $header .= '><a href="'.htmlspecialchars($tab['href']).'"';
                                                    # We don't want to give the watch tab an accesskey if the
                                                    # page is being edited, because that conflicts with the
                                                    # accesskey on the watch checkbox.  We also don't want to
                                                    # give the edit tab an accesskey, because that's fairly su-
                                                    # perfluous and conflicts with an accesskey (Ctrl-E) often
                                                    # used for editing in Safari.
                                                    if( in_array( $action, array( 'edit', 'submit' ) )
                                                    && in_array( $key, array( 'edit', 'watch', 'unwatch' ))) {
                                                            $header .= $skin->tooltip( "ca-$key" );
                                                    } else {
                                                            $header .= $skin->tooltipAndAccesskey( "ca-$key" );
                                                    }
                                                    $header .= '>'.htmlspecialchars($tab['text']).'</a></li>';
                                            }
                                            $header .= "</ul><br><div id='real-content'><div id='inner-content'>{$this->mWikiHeader}{$this->mDidYouMean}";

                                         }//end of if page user
	
					if ($mode) 
					{
						$bocata[$pos].= $header;
						if ($header!=null) $bocata_end = '</div></div>';	
					} else {
						$no_bocata[$pos].= "<div class='home_module' id='wikientry'>$header {$blocks[$i+2]}</div>";
						if ($header!=null) $no_bocata_end = '</div></div>';	
						$output = false;
					}
					break;
			}

			if ($output) {
				if ($mode) $bocata[$pos].=$blocks[$i+2];
				else $no_bocata[$pos].=$blocks[$i+2];
			}
		}

		ksort($bocata); 
		ksort($no_bocata);
		
		$tpl->set( 'bocata', implode("", $bocata).$bocata_end);
		$tpl->set( 'no-bocata', implode("", $no_bocata).$no_bocata_end);
		$tpl->set( 'cloud', $this->mCloud );
		$tpl->set( 'artistDay', $this->mArtistDay);
		$tpl->set( 'genres', $this->mGenres);
		$tpl->set( 'ilikethis', $this->mILikeThis);
		$tpl->set( 'type', $this->mType);
		$tpl->set( 'main_search', $this->mMainSearch);

		return true;
	}

	public function resetOrder() {
		$this->setOrder(0);
	}

	public function setOrder( $order ) {
		$this->mBodytext .= "<!--LAY-O".$order."-->";
	}

	public function switchBocataMode( $mode ) {
		if ($mode xor $this->mInBocata) { $this->mBodytext .= "<!--LAY-B-->"; $this->mInBocata = $mode; } 
	}

	public function setCloud( $val ) {
		$this->mCloud = $val;
	}

	public function setArtistDay($display) {
		$this->mArtistDay = $display;
	}

	public function setGenres($list) {
		$this->mGenres = $list;
	}

	public function setILikeThis($html) {
		$this->mILikeThis = $html;
	}

	public function setDefaultTitle($text) {
		$this->mDefaultTitle = $text;
	}
}
